package com.txby.pf.modules.busi.controller;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.jck.mybatis.util.PageList;
import com.jck.tools.ChkUtil;
import com.txby.pf.common.annotation.SysLog;
import com.txby.pf.common.utils.PageUtils;
import com.txby.pf.common.utils.Query;
import com.txby.pf.common.utils.R;
import com.txby.pf.common.validator.ValidatorUtils;
import com.txby.pf.common.validator.group.UpdateGroup;
import com.txby.pf.modules.busi.entity.BusiVideoEntity;
import com.txby.pf.modules.busi.service.BusiVideoService;
import com.txby.platformApi.component.web.CommonController;
import com.txby.platformCore.component.util.YoukuVideoUtils;

/**
 * 视频
 */
@RestController
@RequestMapping("/busi/video")
public class VideoController extends CommonController {

	@Autowired
	private BusiVideoService busiVideoService;

	/**
	 * 保存视频
	 */
	@SysLog("保存视频")
	@RequestMapping("/save")
	public R save(@RequestParam(required=true) String video_id) {
        CloseableHttpClient httpClient1 =YoukuVideoUtils.createSSLClientDefault();   
        HttpGet httpGet1=null;
        List<NameValuePair> params1=new ArrayList<NameValuePair>();
        params1.add(new BasicNameValuePair("client_id",YoukuVideoUtils.CLIENT_ID));
        params1.add(new BasicNameValuePair("video_id", video_id));
        CloseableHttpResponse response1 = null;
		try {
			httpGet1= YoukuVideoUtils.getHttpGet("https","api.youku.com",-1,"/videos/show.json",params1,null);
			response1 = httpClient1.execute(httpGet1);
	        //打印响应的到的html正文
	        HttpEntity entity1 =response1.getEntity();
	        String html1=EntityUtils.toString(entity1);
	        System.out.println(html1);
			BusiVideoEntity video= new BusiVideoEntity();
			JSONObject.parseObject(html1, BusiVideoEntity.class);
			busiVideoService.save(video);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
	        try {
	        	//关闭连接
	        	response1.close();
				httpClient1.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return R.ok();
	}
	
	
	/**
	 * 视频上传
	 */
	@RequestMapping("/fileUpload")
	public R fileUpload(@RequestParam Map<String, Object> params){
		//查询列表数据
		Query query = new Query(params);
		List<BusiVideoEntity> videoList = busiVideoService.queryList(query);
		int total = busiVideoService.queryTotal(query);
		PageUtils pageUtil = new PageUtils(videoList, total, query.getLimit(), query.getPage());
		return R.ok().put("page", pageUtil);
	}
	
	
	
	/**
	 * 视频列表
	 */
	@RequestMapping("/list")
	@RequiresPermissions("sys:video:info")
	public R list(@RequestParam Map<String, Object> params){
//		数据隔离
		String join="";
		String sql="select * from t_txby_video where 1=1  ";
		String title=params.get("title")==null?"":params.get("title").toString();
		if(!ChkUtil.isEmptyAllObject(title)){
			if (title.contains("%") || title.contains("_")) {
				title = title.trim().replace("%", "\\%").replace("_", "\\_");
				title = "%" + title + "%";
				sql += " and title like #{pojo.title} escape '\\'";
				params.put("title", "%" + title.trim() + "%");
			} else {
				title = "%" + title.trim() + "%";
				sql += " and title like #{pojo.title} ";
				params.put("title", "%" + title.trim() + "%");
			}
		}
		sql += Query.getLastSortSql(params, " order by updateTime ");
		//查询列表数据
		PageList<Map> pl = this.mBaseDao.findBySqlForPageList(sql, Map.class, Query.getPageBean(params), params);
		PageUtils pageUtil = new PageUtils(pl.getResultList(), pl.getPageBean().getTotalCount().intValue(),  pl.getPageBean().getPageSize(), pl.getPageBean().getPage());
		return R.ok().put("page", pageUtil);
	}
	
	/**
	 * 获取登录的用户信息
	 */
	@RequestMapping("/info/{videoId}")
	public R info(@PathVariable("videoId") Long videoId){
		BusiVideoEntity video = busiVideoService.queryObject(videoId);
		return R.ok().put("video", video);
		
	}
	
	
	/**
	 * 修改视频
	 */
	@SysLog("修改视频")
	@RequestMapping("/update")
	public R update(@RequestParam("title") String title,@RequestParam("subhead") String subhead,@RequestParam("videoType") String videoType,
			@RequestParam("content") String content,@RequestParam("videoId") String videoId){
		BusiVideoEntity video= new BusiVideoEntity();
		ValidatorUtils.validateEntity(video, UpdateGroup.class);
		busiVideoService.update(video);
		return R.ok();
	}
	
	/**
	 * 删除视频
	 */
	@SysLog("视频删除")
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] videoIds){
		busiVideoService.deleteBatch(videoIds);
		return R.ok();
	}
}
